#include "KeyBinder.h"

KeyBinder* KeyBinder::instance = new KeyBinder();

KeyBinder* KeyBinder::getInstance() {
    return instance;
}

KeyBinder::KeyBinder() {
    loadBindings();
}

KeyBinder::~KeyBinder() {
}

void KeyBinder::setWindow(sf::Window* aWindow) {
    window = aWindow;
}

void KeyBinder::loadBindings() {
    bindings[LEFT] = new Key(sf::Keyboard::A);
    bindings[RIGHT] = new Key(sf::Keyboard::D);
    bindings[FORWARD] = new Key(sf::Keyboard::W);
    bindings[BACKWARD] = new Key(sf::Keyboard::S);
    bindings[ACCELERATE] = new Key(sf::Keyboard::Z);
    bindings[DECELERATE] = new Key(sf::Keyboard::X);
    bindings[SHOOT] = new Key(sf::Keyboard::Space);
}

void KeyBinder::update() {
    typedef std::map<InputKey, Action*>::iterator it_type;

    for(it_type iterator = bindings.begin(); iterator != bindings.end(); iterator++) {
        iterator->second->update();
    }
}

void KeyBinder::bindKey(InputKey inputKey, sf::Keyboard::Key newKey) {
    delete bindings[inputKey];
    bindings[inputKey] = new Key(newKey);
}

bool KeyBinder::isKeyPressed(InputKey key) {
    return bindings[key]->isPressed();
}

bool KeyBinder::isKeyTapped(InputKey key) {
    return bindings[key]->isTapped();
}
